package com.party.mobile.web.controller.partner;

import com.google.common.base.Strings;
import com.party.common.utils.StringUtils;
import com.party.core.exception.BusinessException;
import com.party.mobile.biz.partner.PartnerBizService;
import com.party.mobile.web.dto.AjaxResult;
import com.party.mobile.web.dto.partner.input.GetPartnerIdInput;
import com.party.mobile.web.dto.partner.output.GetDetailsOutput;
import com.party.mobile.web.dto.partner.output.GetGoodsOutput;
import com.party.common.utils.PartyCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 合作商控制层
 * party
 * Created by wei.li
 * on 2016/10/24 0024.
 */

@Controller
@RequestMapping(value = "/partner/goods")
public class PartnerController {


    @Autowired
    PartnerBizService partnerBizService;

    Logger logger = LoggerFactory.getLogger(getClass());
    /**
     * 获取合作商商品
     * @param id 商铺商品编号
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "/getGoods/{storeGoodsId}")
    public AjaxResult getGoods(@PathVariable(value = "storeGoodsId") String id){

        //数据验证
        if (Strings.isNullOrEmpty(id)){
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "参数验证不通过");
        }

        GetGoodsOutput getGoodsOutput;
        try {
            getGoodsOutput = partnerBizService.getGoods(id);
        } catch (BusinessException be) {
            return AjaxResult.error(be.getCode(), be.getMessage());
        }
        return AjaxResult.success(getGoodsOutput);
    }


    /**
     * 获取合作商商品详情
     * @param id 商铺商品主键
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "/getDetails/{storeGoodsId}")
    public AjaxResult getDetails(@PathVariable(value = "storeGoodsId") String id){

        //数据验证
        if (Strings.isNullOrEmpty(id)){
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "参数验证不通过");
        }

        GetDetailsOutput getDetailsOutput;
        try {
             getDetailsOutput = partnerBizService.getDetails(id);
        } catch (BusinessException be) {
            return AjaxResult.error(be.getCode(), be.getMessage());
        }
        return AjaxResult.success(getDetailsOutput);
    }
    /**
     * 通过模块及业务id获取机构id
     *
     * @param getPartnerIdInput 输入参数
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getPartnerIdByBusiness")
    public AjaxResult getPartnerIdByBusiness(GetPartnerIdInput getPartnerIdInput){
        String partnerId;
        //数据验证
        if (StringUtils.isBlank(getPartnerIdInput.getModule())){
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "module为空");
        }
        if (null==getPartnerIdInput.getParams()||
                ("".equals(getPartnerIdInput.getParams().getId())
                 &&"".equals(getPartnerIdInput.getParams().getActId())
                )){
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "参数验证不通过");
        }
        try {
            partnerId = partnerBizService.getPartnerIdByBusiness(getPartnerIdInput);
        } catch (BusinessException be) {
            logger.error("通过模块及业务id获取机构id接口异常",getPartnerIdInput, be);
            return AjaxResult.error(be.getCode(), be.getMessage());
        }

       return AjaxResult.success((Object)partnerId);
    }
}
